package com.tongji.housediy.view.components
{
	import caurina.transitions.Tweener;
	
	import com.tongji.housediy.events.commonEvent.SetCompStateEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideFilterEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.utils.CollisionDetection;
	import com.tongji.housediy.utils.MathCalculation;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.ui.Mouse;
	import flash.ui.MouseCursor;
	import flash.utils.ByteArray;
	
	import mx.core.UIComponent;

	public class GridContainer extends RoomShape
	{
		private var gap:int;
		private var tickRectScale:int;
		public var webHeight:int;
		public var webWidth:int;
		
		public var isMoveLeft:Boolean;
		public var isMoveRight:Boolean;
		public var isMoveUp:Boolean;
		public var isMoveDown:Boolean;
		public var wheelScale:Number;
		
		// 保存全局唯一的用于旋转 房间、地板、家具 的 RotationBtn
		public var rotationBtn:RotationBtn;
		
		// 信息菜单的连接线
		public var infoLine:InfoLine;
		
		// stage 是否按下 X/Y 键
		public var isXKeyDown:Boolean = false;
		public var isYKeyDown:Boolean = false;
		
		[Embed("assets/images/centerCursor.png")]
		private var CursorData:Class;
		public var centerCursor:Bitmap = new CursorData();
		
		[Embed("assets/images/horizontalCursor.png")]
		private var HorCursorData:Class;
		public var horizontalCursor:Bitmap = new HorCursorData();
		
		[Embed("assets/images/verticalCursor.png")]
		private var VerCursorData:Class;
		public var verticalCursor:Bitmap = new VerCursorData();
		
		public var cursorBitmap:Bitmap;
		
		public var rulerContainer:Sprite;
		
		public var roomNameContainer:RoomNameContainer;
		
		public function GridContainer()
		{
			super();
			
			gap = 32;
			tickRectScale = 5;
			webHeight = 6000;
			webWidth = 8000;
			
			drawRect();
			drawTickline();
			drawThinline();
			
			initFunctionChild();
		}
		
		private function drawRect():void
		{
			graphics.beginFill(0xEFEFEF);
			graphics.drawRect(0,0,webWidth,webHeight);
			graphics.endFill();
		}
		
		/**
		 *画画布格子里的粗线 
		 * 
		 */		
		private function drawTickline():void
		{
			var large_gap:int = gap * tickRectScale;
			var countWidth:int = webWidth / large_gap;
			var countHeight:int = webHeight / large_gap;
			
			graphics.lineStyle(0.5, 0x787878, 0.3);
			
			for(var i:int = 1; i <= countWidth; i++)
			{
				var temp:int = i * large_gap;
				
				graphics.moveTo(temp, 0);
				graphics.lineTo(temp, webHeight);
			}
			
			for(var j:int = 1; j <= countHeight; j++)
			{
				var temp2:int = j * large_gap;
				
				
				graphics.moveTo(0, temp2);
				graphics.lineTo(webWidth, temp2);
			}
			
		}
		
		/**
		 * 画画布格子里的浅的细线
		 * 
		 */		
		private function drawThinline():void
		{
			var large_gap:int = gap*tickRectScale;
			var countWidth:int = webWidth / large_gap;
			var countHeight:int = webHeight / large_gap;
			var count:int = tickRectScale - 1;
			
			graphics.lineStyle(0.5, 0xBCBCBC, 0.3);
			
			for(var i:int = 1; i <= countWidth; i++)
				for(var j:int = 1; j <= count; j++)
				{
					var temp:int = j * gap + (i - 1) * large_gap;
					
					graphics.moveTo(temp, 0);
					graphics.lineTo(temp, webHeight);
				}	
			
			for(var m:int = 1; m <= countHeight; m++)
				for(var n:int = 1; n <= count; n++)
				{
					var temp2:int = n * gap + (m - 1) * large_gap;
					
					graphics.moveTo(0, temp2);
					graphics.lineTo(webWidth, temp2);
				}
		}
		
		private function initFunctionChild():void
		{
			rotationBtn = new RotationBtn();
			rulerContainer = new Sprite();
			roomNameContainer = new RoomNameContainer();
		}
		
		public function moveGridContainer(offsetX:Number, offsetY:Number):void
		{
			var multipler:Number = MathCalculation.getZoomMultiple();
			this.x += offsetX * multipler;
			this.y += offsetY * multipler;
		}
		
		public function scaleGridContainer(parentMouseX:Number, parentMouseY:Number, scaleValue:Number):void
		{
			/*Tweener.addTween(this, {x: parentMouseX + (this.x - parentMouseX) * scaleValue, 
				y: parentMouseY + (this.y - parentMouseY) * scaleValue,
				scaleX: this.scaleX * scaleValue,
				scaleY: this.scaleY * scaleValue,
				time: 0.2});*/
			
			this.x = parentMouseX + (this.x - parentMouseX) * scaleValue;
			this.y = parentMouseY + (this.y - parentMouseY) * scaleValue;
			this.scaleX *= scaleValue;
			this.scaleY *= scaleValue;
		}
		
		override protected function mouseDownHandler(event:MouseEvent):void
		{
			hideRotationBtn();
			
			// 隐藏 InfoWindow
			this.dispatchEvent(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW));
			this.dispatchEvent(new ShowHideFilterEvent(ShowHideFilterEvent.HIDE_FILTER, this));
			this.dispatchEvent(new SetCompStateEvent(SetCompStateEvent.RESET_FILTER));
			// 隐藏所有的 RoomVertex
			for (var i:int = 0; i < this.numChildren; i++)
			{
				if (this.getChildAt(i) is AbstractDrawingRoom)
					(this.getChildAt(i) as AbstractDrawingRoom).hideVertex();
			}
			
			super.mouseDownHandler(event);
		}
		
		override protected function enterFrameHandler(event:Event):void
		{
			super.enterFrameHandler(event);
			this.moveGridContainer(offsetX, offsetY);
		}
		
		// 移除场景中的 rotationBtn
		public function hideRotationBtn():void
		{
			if (this.rotationBtn && this.contains(this.rotationBtn))
				this.removeChild(this.rotationBtn);
		}
		
		// 供 FreeFormComponent 调用，用于创建 多边形 IndependentFloor
		public function createFreeFloor(vertices:Vector.<Number>, freeFormComp:FreeFormComponent, offsetX:Number, offsetY:Number):void
		{
			// 清楚  FreeFormBtn 按钮 上的滤镜
			this.dispatchEvent(new SetCompStateEvent(SetCompStateEvent.RESET_FILTER));
			
			var floor:IndependentFloor = new IndependentFloor(clone(vertices));
			this.addChild(floor);
			floor.parentGridContainer = this;
			floor.x = offsetX;
			floor.y = offsetY;
			
			this.removeChild(freeFormComp);
			freeFormComp = null;
			
			Mouse.cursor = MouseCursor.AUTO;
			
			setRoomPosition(floor, offsetX, offsetY, floor.mouseX, floor.mouseY);
		}
		
		public function removeTempFreeFormComponent(freeFormComp:FreeFormComponent):void
		{
			this.removeChild(freeFormComp);
			freeFormComp = null;
			
			// 清楚  FreeFormBtn 按钮 上的滤镜
			this.dispatchEvent(new SetCompStateEvent(SetCompStateEvent.RESET_FILTER));
			
			Mouse.cursor = MouseCursor.AUTO;
		}
		
		public function setRoomPosition(floor:IndependentFloor, offsetX:Number, offsetY:Number, plusX:Number = 0, plusY:Number = 0):void
		{
			this.addChild(floor);
			floor.x = this.mouseX - plusX;
			floor.y = this.mouseY - plusY;
			
			var floorRec:Rectangle = floor.getBounds(floor.root);
			
			for (var i:int = 0; i < this.numChildren; i++)
			{
				if (!(this.getChildAt(i) is IndependentFloor) && !(this.getChildAt(i) is RotationBtn))
				{
					var collisionRec:Rectangle = CollisionDetection.intersectionRectangle(floor, this.getChildAt(i));
					if (collisionRec.width == floorRec.width && collisionRec.height == floorRec.height)
					{
						(this.getChildAt(i) as AbstractDrawingRoom).roomFloor.addChildAt(floor, 0);
						floor.x = offsetX - (this.getChildAt(i) as AbstractDrawingRoom).x;
						floor.y = offsetY - (this.getChildAt(i) as AbstractDrawingRoom).y;
						break;
					}
				}
			}
		}
		
		private function clone(source:Object):*
		{
			var myBA:ByteArray = new ByteArray();
			myBA.writeObject(source);
			myBA.position = 0;
			return (myBA.readObject());
		}
		
		public function setChildRoomInfo(roomHeight:int):void
		{
			for (var i:int = 0; i < this.numChildren; i++)
			{
				if (this.getChildAt(i) is AbstractDrawingRoom)
					(this.getChildAt(i) as AbstractDrawingRoom).roomHeight = roomHeight;
			}
		}
	}
}